package com.campus.controller;

import cn.hutool.core.bean.BeanUtil;
import com.campus.common.config.CodeConfig;
import com.campus.entity.File.File;
import com.campus.entity.ResultBody;
import com.campus.entity.vo.AuditVO;
import com.campus.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: AlaskaBoo
 * @Date: 2022-11-11 12:06
 **/
@RestController
@RequestMapping("/api/audit")
@Transactional
public class AuditController {

    @Autowired
    private FileService fileService;

    /**
     * 文件审核
     *
     * @param id
     * @param isShow
     * @return
     */
    @PutMapping("/updateIsShow/{id}/isShow={isShow}")
    @CacheEvict(value = "Audit", allEntries =true)
    public ResultBody uploadQuery(@PathVariable(value = "id") Integer id, @PathVariable(value = "isShow") Integer isShow) {
        File file = fileService.getById(id);
        if (file == null) {
            return ResultBody.error(CodeConfig.QUERY_FAIL).message(CodeConfig.QUERY_FAIL.message);
        }
        file.setIsShow(isShow);
        fileService.saveOrUpdate(file);
        return ResultBody.ok().message(CodeConfig.PIC_HAVE_QUEUE.message);
    }

    /**
     * 查询审核通过文件
     *
     * @return
     */
    @GetMapping("/fileAuditPass")
    @Cacheable(value = "Audit")
    public ResultBody fileAuditPass() {
        List<File> fileList = fileService.lambdaQuery().eq(File::getIsShow, 1).list();
        return getAudit(fileList);
    }

    /**
     * 查询审核不通过文件
     *
     * @return
     */
    @GetMapping("/fileAuditInPass")
    @Cacheable(value = "Audit")
    public ResultBody fileAuditInPass() {
        List<File> fileList = fileService.lambdaQuery().eq(File::getIsShow, 2).list();
        return getAudit(fileList);
    }

    /**
     * 查询未审核
     *
     * @return
     */
    @GetMapping("/fileNoAudit")
    @Cacheable(value = "Audit")
    public ResultBody fileNoAudit() {
        List<File> fileList = fileService.lambdaQuery().eq(File::getIsShow, 0).list();
        return getAudit(fileList);
    }

    private ResultBody getAudit(List<File> fileList) {
        List<AuditVO> list = new ArrayList<>();
        fileList.forEach(file1 -> {
            AuditVO auditVO = new AuditVO();
            BeanUtil.copyProperties(file1, auditVO);
            list.add(auditVO);
        });
        return ResultBody.ok().data("fileList", list);
    }

}
